Passed
Push — master ( 7018d4...d19e24 )
by EMP
02:46
created

main.js ➔ addMsg   B

Complexity

Conditions 8

Size

Total Lines 63
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 28
dl 0
loc 63
rs 7.3333
c 0
b 0
f 0
cc 8

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
"use strict";
2
3
sodium.ready.then(function() {
1 ignored issue
show
Bug introduced by
The variable sodium seems to be never declared. If this is a global, consider adding a /** global: sodium */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
4
5
const ae = new AllEars(function(ok) {
1 ignored issue
show
Bug introduced by
The variable AllEars seems to be never declared. If this is a global, consider adding a /** global: AllEars */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
6
	if (ok) {
7
		document.getElementById("txt_skey").style.background = "#404b41";
8
		document.getElementById("txt_skey").maxLength = "64";
9
	} else {
10
		console.log("Failed to load All-Ears");
11
	}
12
});
13
14
let page=0;
15
16
// Helper functions
17
function getCountryName(countryCode) {
18
	const opts = document.getElementById("gatekeeper_country");
19
20
	for (let i = 0; i < opts.length; i++) {
21
		if (opts[i].value === countryCode) {
22
			return opts[i].textContent;
23
		}
24
	}
25
26
	return "Unknown countrycode: " + countryCode;
27
}
28
29
function getCountryFlag(countryCode) {
30
	const regionalIndicator1 = 127462 + countryCode.codePointAt(0) - 65;
31
	const regionalIndicator2 = 127462 + countryCode.codePointAt(1) - 65;
32
	return "&#" + regionalIndicator1 + ";&#" + regionalIndicator2 + ";";
33
}
34
35
function displayMsg(isInt, num) {
36
	document.getElementById("msg").hidden = false;
37
	document.getElementById("msg").getElementsByTagName("h1")[0].textContent = isInt ? ae.GetIntMsgTitle(num) : ae.GetExtMsgTitle(num);
38
	document.getElementById("msg").getElementsByTagName("pre")[0].textContent = isInt ? ae.GetIntMsgBody(num) : ae.GetExtMsgBody(num);
39
40
	document.getElementById("readmsg_to").textContent = isInt ? ae.GetIntMsgTo(num) : ae.GetExtMsgTo(num);
41
42
	const ts = isInt? ae.GetIntMsgTime(num) : ae.GetExtMsgTime(num);
43
	document.getElementById("readmsg_date").textContent = new Date(ts * 1000).toISOString().slice(0, 16).replace("T", " ");
44
45
	if (!isInt) {
46
		const cc = ae.GetExtMsgCountry(num);
47
48
		document.getElementById("readmsg_greet").textContent = ae.GetExtMsgGreet(num);
49
		document.getElementById("readmsg_ip").textContent = ae.GetExtMsgIp(num);
50
		document.getElementById("readmsg_tls").textContent = ae.GetExtMsgTLS(num);
51
		document.getElementById("readmsg_country").innerHTML = getCountryFlag(cc) + " " + getCountryName(cc);
52
		document.getElementById("readmsg_envfrom").textContent = ae.GetExtMsgFrom(num);
53
54
		let flagText = "";
55
		if (!ae.GetExtMsgFlagPExt(num)) flagText += "<abbr title=\"The sender did not use the Extended (ESMTP) protocol\">SMTP</abbr> ";
56
		if (!ae.GetExtMsgFlagQuit(num)) flagText += "<abbr title=\"The sender did not issue the required QUIT command\">QUIT</abbr> ";
57
		if (ae.GetExtMsgFlagRare(num)) flagText += "<abbr title=\"The sender issued unusual command(s)\">RARE</abbr> ";
58
		if (ae.GetExtMsgFlagFail(num)) flagText += "<abbr title=\"The sender issued invalid command(s)\">FAIL</abbr> ";
59
		if (ae.GetExtMsgFlagPErr(num)) flagText += "<abbr title=\"The sender violated the protocol\">PROT</abbr> ";
60
		document.getElementById("readmsg_flags").innerHTML = flagText.trim();
61
	} else {
62
		document.getElementById("readmsg_from").textContent = ae.GetIntMsgFrom(num);
63
	}
64
65
}
66
67
// Interface
68
function addMsg(isInt, i) {
69
	const inbox = document.getElementById("tbl_inbox");
70
	const sent = document.getElementById("tbl_sent");
71
72
	const isSent = isInt? ae.GetIntMsgIsSent(i) : false;
73
	const table = isSent ? sent : inbox;
74
75
	const row = table.insertRow(-1);
76
	const cellTime = row.insertCell(-1);
77
	const cellSubj = row.insertCell(-1);
78
	const cellSnd1 = row.insertCell(-1);
79
	const cellSnd2 = row.insertCell(-1);
80
81
	const ts = isInt? ae.GetIntMsgTime(i) : ae.GetExtMsgTime(i);
82
	cellTime.setAttribute("data-ts", ts);
83
	cellTime.textContent = new Date(ts * 1000).toISOString().slice(0, 16).replace("T", " ");
84
85
	cellSubj.textContent = isInt? ae.GetIntMsgTitle(i) : ae.GetExtMsgTitle(i);
86
87
	if (isInt) {
88
		cellSnd1.textContent = ae.GetIntMsgFrom(i);
89
		cellSnd1.className = (ae.GetIntMsgFrom(i).length === 24) ? "mono" : "";
90
	} else {
91
		const from1 = ae.GetExtMsgFrom(i);
92
		const from2 = from1.substring(from1.indexOf("@") + 1);
93
		const cc = ae.GetExtMsgCountry(i);
94
95
		cellSnd1.textContent = from1.substring(0, from1.indexOf("@"));
96
		cellSnd2.innerHTML = "<abbr title=\"" + getCountryName(cc) + "\">" + getCountryFlag(cc) + "</abbr>";
97
98
		const fromText = document.createElement("span");
99
		fromText.textContent = " " + from2;
100
		cellSnd2.appendChild(fromText);
101
	}
102
103
//	divDel.innerHTML = "<input class=\"delMsg\" type=\"checkbox\" data-id=\"" + ae.GetIntMsgIdHex(i) + "\">";
104
105
	cellSubj.onclick = function() {
106
		displayMsg(isInt, i);
107
	};
108
/*
109
	cellDel.children[0].onchange = function() {
110
		if (!divDel.children[0].checked) {
111
			const checkboxes = elmt.getElementsByTagName("input");
112
			let checked = false;
113
114
			for (let j = 0; j < checkboxes.length; j++) {
115
				if (checkboxes[j].checked) {
116
					checked = true;
117
					break;
118
				}
119
			}
120
121
			if (!checked) {
122
				document.getElementById(isSent ? "btn_sentdel" : "btn_msgdel").hidden = true;
123
				return;
124
			}
125
		}
126
127
		document.getElementById(isSent? "btn_sentdel" : "btn_msgdel").hidden = false;
128
	};
129
*/
130
}
131
132
function addMessages() {
133
	const maxExt = ae.GetExtMsgCount();
134
	const maxInt = ae.GetIntMsgCount();
135
136
	let numExt = 0;
137
	let numInt = 0;
138
139
	for (let i = 0; i < (page * 20) + 20; i++) {
140
		const tsInt = (numInt < maxInt) ? ae.GetIntMsgTime(numInt) : 0;
141
		const tsExt = (numExt < maxExt) ? ae.GetExtMsgTime(numExt) : 0;
142
		if (tsInt === 0 && tsExt === 0) break;
143
144
		if (tsInt !== 0 && (tsExt === 0 || tsInt > tsExt)) {
145
			if (i < (page * 20)) {
146
				numInt++;
147
				continue;
148
			}
149
150
			addMsg(true, numInt);
151
			numInt++;
152
		} else if (tsExt !== 0) {
153
			if (i < (page * 20)) {
154
				numExt++;
155
				continue;
156
			}
157
158
			addMsg(false, numExt);
159
			numExt++;
160
		}
161
	}
162
}
163
164
function clearMessages() {
165
	document.getElementById("tbl_inbox").innerHTML = "";
166
//	document.getElementById("tbl_sentm").innerHTML = "";
167
//	document.getElementById("tbl_notes").innerHTML = "";
168
//	document.getElementById("tbl_files").innerHTML = "";
169
}
170
171
function reloadInterface() {
172
	document.getElementById("div_begin").hidden = true;
173
	document.getElementById("div_main").style.display = "grid";
174
}
175
176
document.getElementById("btn_refresh").onclick = function() {
177
	const btn = this;
178
	btn.disabled = true;
179
180
	ae.Message_Browse(0, function(successBrowse) {
181
		if (successBrowse) {
182
			clearMessages();
183
			addMessages();
184
			btn.disabled = false;
185
		} else {
186
			console.log("Failed to refresh");
187
			btn.disabled = false;
188
		}
189
	});
190
};
191
192
document.getElementById("txt_skey").onkeyup = function(event) {
193
	if (event.key === "Enter") {
194
		event.preventDefault();
195
		document.getElementById("btn_enter").click();
196
	}
197
};
198
199
document.getElementById("btn_enter").onclick = function() {
200
	const txtSkey = document.getElementById("txt_skey");
201
	if (!txtSkey.reportValidity()) return;
202
203
	const btn = this;
204
	btn.disabled = true;
205
	document.getElementById("txt_skey").style.background = "#111";
206
207
	ae.SetKeys(txtSkey.value, function(successSetKeys) {
208
		if (successSetKeys) {
209
			ae.Account_Browse(0, function(successBrowse) {
210
				if (successBrowse) {
211
					txtSkey.value = "";
212
					reloadInterface();
213
					document.getElementById("btn_refresh").click();
214
				} else {
215
					console.log("Failed to enter");
216
					btn.disabled = false;
217
					document.getElementById("txt_skey").style.background = "#404b41";
218
					txtSkey.focus();
219
				}
220
			});
221
		} else {
222
			console.log("Invalid format for key");
223
			btn.disabled = false;
224
			document.getElementById("txt_skey").style.background = "#404b41";
225
			txtSkey.focus();
226
		}
227
	});
228
};
229
230
});
231